<template>
  <div class="container">
    <div class="content">
      <div class="topBox">
        <div class="left">
          <el-button  v-permission="'departmentManagementAdd'" class="btnColor" @click="addMenu()">添加+</el-button>
        </div>
      </div>
      <div class="tableBox">
        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          row-key="id"
          border
          default-expand-all
          :tree-props="{
            children: 'departmentVoList',
            hasChildren: 'hasChildren',
          }"
        >
          <el-table-column align="center" prop="name" label="部门名称" />

          <el-table-column align="center" prop="createTime" label="创建时间" />
          <el-table-column align="center" prop="remarks" label="备注" />
          <el-table-column
            align="center"
            prop="address"
            label="操作"
            width="260"
          >
            <template slot-scope="scope">
              <div class="btnBox">
                <div
                  slot="reference"
                  class="editBtn1"
                  @click="addMenu(scope.row)"
                  v-permission="'departmentManagementDetail'"
                >
                  <i class="el-icon-edit" />查看详情
                </div>
                <div
                  slot="reference"
                  class="editBtn3"
                  type="text"
                  @click="delDetail(scope.row)"
                  v-permission="'departmentManagementDelete'"
                >
                  <i class="el-icon-delete" />
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog :title="name" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="100px">
        <div class="formItem">
          <el-form-item label="部门名称">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="上级部门">
            <treeselect
              v-model="form.parentId"
              :options="menuOptions"
              :show-count="true"
              placeholder="请选择上级部门"
              :normalizer="normalizer"
              @select="handleSelect"
              default-expand-level="Infinity"
            />
          </el-form-item>
        </div>

        <div class="dialogFooter">
          <el-form-item>
            <el-button class="rightBtn" @click="onSubmitMenu"
              >立即创建</el-button
            >
            <el-button class="leftBtn" @click="cancelMenu">取消</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  departmentList,
  departmentAdd,
  departmentUpdate,
  departmentDelete,
} from "@/api/systemManagement";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { handleTree } from "@/utils/transListToTreeData";
export default {
  name: "MenuManagement",
  components: { Treeselect },
  data() {
    return {
      searchInput: "",
      tableData: [],
      titleName: "组织管理",
      loading: true,
      dialogVisible: false,
      name: "信号",
      menuOptions: [],
      form: {
        name: "",
        parentId: null,
      },
    };
  },
  created() {
    this.departmentListApi();
  },
  methods: {
    async departmentListApi() {
      this.loading = true;
      const res = await departmentList();
      this.GetOrganizeList();

      this.tableData = res.data;
      this.loading = false;
    },
    async GetOrganizeList() {
      const res = await departmentList();
      this.menuOptions = [];
      const menu = {
        id: 0,
        name: "美欣达印染园区安全管理平台",
        departmentVoList: [],
        parentId: -1,
      };
      menu.departmentVoList = handleTree(res.data, "id");
      this.menuOptions.push(menu);
    },
    normalizer(node) {
      if (node.departmentVoList && !node.departmentVoList.length) {
        delete node.departmentVoList;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.departmentVoList,
      };
    },
    async addMenu(row) {
      this.dialogVisible = true;
      if (row) {
        this.name = "编辑部门";
        this.form = row;
      } else {
        this.name = "新增部门";
        this.form = {
          id: null,
          name: "",
          parentId: null,
        };
      }
    },
    async delDetail(row) {
      this.$confirm(`此操作将永久删除   ${row.name}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });

          const res = await departmentDelete({ id: row.id });
          console.log(res);
          this.departmentListApi();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async onSubmitMenu() {
      if (!this.form.id) {
        const res = await departmentAdd({
          name: this.form.name,
          parentId: this.form.parentId,
        });
        if (res.code == 0) {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.departmentListApi();
          this.cancelMenu();
        }
      } else {
        const res = await departmentUpdate({
          id: this.form.id,
          name: this.form.name,
          parentId: this.form.parentId,
        });
        if (res.code == 0) {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.departmentListApi();
          this.cancelMenu();
        }
      }
    },
    cancelMenu() {
      this.dialogVisible = false;
      this.form = {
        id: null,
        name: "",
        parentId: null,
      };
    },
    handleSelect(node) {
      this.form.parentId = node.parentId;
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  background-color: #f8fafc;
  padding: 10px;

  .btnColor {
    background-color: #0077FF;
    color: #fff;
    margin-left: 10px;
  }
  .content {
    background-color: #fff;
    padding: 20px 10px;
    .topBox {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 40px;
      .right {
        display: flex;
      }
    }
    .tableBox {
      padding: 10px 40px;
      ::v-deep .el-table th,
      .el-table tr {
        background-color: #f6f9f8 !important;
        color: #333 !important;
        height: 30px;
      }
      ::v-deep .el-table__row {
        height: 50px !important;
        padding: 0 !important;
      }
      ::v-deep .el-table th,
      .el-table tr {
        height: 50px !important;
      }
      ::v-deep .el-table td,
      .el-table th {
        padding: 0 !important;
        padding-top: 5px !important;
      }

      .btnBox {
        display: flex;
        align-items: center;
        justify-content: center;

        .editBtn1 {
          cursor: pointer;
          background-color: #0077FF;
          color: #fff;
          // width: 25px;
          padding: 0 10px;
          height: 25px;
          line-height: 25px;
          text-align: center;
          border-radius: 5px;
        }
        .editBtn2 {
          cursor: pointer;
          margin-left: 10px;
          background-color: #ffa426;
          padding: 0 10px;
          color: #fff;
          height: 25px;
          line-height: 25px;
          text-align: center;
          border-radius: 5px;
        }
        .editBtn3 {
          cursor: pointer;
          margin-left: 10px;
          background-color: #d72e2e;
          width: 25px;
          color: #fff;
          height: 25px;
          line-height: 25px;
          text-align: center;
          border-radius: 5px;
        }
      }

      .footerPage {
        padding: 0 30px 0 0;
        display: flex;
        justify-content: right;
        width: 100%;
        background-color: #f8fafc;
        height: 50px;
        align-items: center;
        margin-top: 10px;

        //  ::v-deep .el-pagination .btn-next, .el-pagination .btn-prev{
        //   background-color: #087 !important;
        // }
      }
    }
  }

  .rightBtn {
    background-color: #0077FF;
    color: #fff;
  }
  .leftBtn {
    border: 1px solid #0077FF;
    color: #0077FF;
  }
}
</style>
